package org.chameleon.limit.convert;

import com.alibaba.fastjson.JSON;
import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import org.chameleon.datasource.convert.AbstractConverter;
import org.chameleon.limit.entity.RateLimiterConfig;

import java.util.ArrayList;
import java.util.List;

public class ApolloLimitChangeConvert extends AbstractConverter<ConfigChangeEvent, List<RateLimiterConfig>> {


    public ApolloLimitChangeConvert() {
        super(source->{
            List<RateLimiterConfig> limiterConfigList = new ArrayList<>(source.changedKeys().size());
            for (String key : source.changedKeys()) {
                ConfigChange change = source.getChange(key);
                final RateLimiterConfig jsonObject = JSON.parseObject(change.getNewValue(),RateLimiterConfig.class);
                limiterConfigList.add(jsonObject);
            }
            return limiterConfigList;
            },null);
    }
}
